package com.hilton.tigershark.downloads;

import java.io.File;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;

public class DownloadInfo implements Parcelable {
    private String mUrl;
    private String mBasePath;
    private String mFilename;
    private String mUserAgent;
    private String mMimeType;
    private int mContentLength;
    public DownloadInfo(String url, String bp, String fn, String ua, String mime, int cl) {
        mUrl = url;
        mBasePath = bp;
        mFilename = fn;
        mUserAgent = ua;
        mMimeType = mime;
        mContentLength = cl;
    }
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mUrl);
        dest.writeString(mBasePath);
        dest.writeString(mFilename);
        dest.writeString(mUserAgent);
        dest.writeString(mMimeType);
        dest.writeInt(mContentLength);
    }
    
    public static final Parcelable.Creator<DownloadInfo> CREATOR = new Parcelable.Creator<DownloadInfo>() {
	public DownloadInfo createFromParcel(Parcel in) {
	    return new DownloadInfo(in);
	}
	
	public DownloadInfo[] newArray(int size) {
	    return new DownloadInfo[size];
	}
    };
    
    private static final String TAG = "DownloadInfo";
    
    private DownloadInfo(Parcel in) {
        mUrl = in.readString();
        mBasePath = in.readString();
        mFilename = in.readString();
        mUserAgent = in.readString();
        mMimeType = in.readString();
        mContentLength = in.readInt();
    }
    
    public boolean empty() {
        return TextUtils.isEmpty(mUrl);
    }
    public String getUrl() {
        return mUrl;
    }
    public String getBasePath() {
        return mBasePath;
    }
    public String getFilename() {
        return mFilename;
    }
    public String getUserAgent() {
        return mUserAgent;
    }
    public String getMimeType() {
        return mMimeType;
    }
    public long getContentLength() {
        return mContentLength;
    }
    
    public String getFilePath() {
	ensureFilename();
	return mBasePath + File.separator + mFilename;
    }
    
    public void ensureFilename() {
	Log.e(TAG, "old filenmae is [" + mFilename + "]");
	int dot = mFilename.indexOf(".");
	String extension = "";
	String filename = mFilename;
	if (dot > 0) {
	    filename = mFilename.substring(0, dot);
	    extension = mFilename.substring(dot + 1);
	}
	File bp = new File(mBasePath);
	if (!bp.exists()) {
	    if (!bp.mkdirs()) {
		Log.e(TAG, "failed to create dirs for basepath " + bp);
	    }
	    return;
	}
	List<String> entries = Arrays.asList(bp.list());
	Log.e(TAG, "entries are " + entries);
	int suffix = 0;
	while (entries.contains(mFilename) && suffix < 100) {
	    suffix++;
	    mFilename = filename + "_" + suffix + "." + extension;
	}
	Log.e(TAG, "new filename is [" + mFilename + "]");
    }
    @Override
    public String toString() {
	return "DownloadInfo [mUrl=" + mUrl + ", mBasePath=" + mBasePath
		+ ", mFilename=" + mFilename + ", mUserAgent=" + mUserAgent
		+ ", mMimeType=" + mMimeType + ", mContentLength="
		+ mContentLength + "]";
    }
    
    public Uri createDatabaseRecord(ContentResolver cr) {
	final ContentValues values = new ContentValues();
	values.put(Downloads.URL, mUrl);
	values.put(Downloads.PATH, mBasePath + File.separator + mFilename);
	values.put(Downloads.FILENAME, mFilename);
	values.put(Downloads.MIME_TYPE, mMimeType);
	values.put(Downloads.TOTAL_BYTES, mContentLength);
	values.put(Downloads.CREATED, new GregorianCalendar().getTimeInMillis());
	values.put(Downloads.STATUS, Downloads.DownloadStatus.STATUS_DOWNLOADING);
	return cr.insert(Downloads.CONTENT_URI, values);
    }
}